/*
 * Copyright (c) 2019, Zhejiang uniview Technologies Co., Ltd. All rights reserved.
 * <http://www.uniview.com/>
 *------------------------------------------------------------------------------
 * Product     :
 * Module Name :
 * Date Created: 2020-04-07 10:42
 * Creator     : s04180
 * Description :
 *
 *------------------------------------------------------------------------------
 * Modification History
 * DATE        NAME             DESCRIPTION
 *------------------------------------------------------------------------------
 *
 *------------------------------------------------------------------------------
 */
package com.kyg.tjtoolmanager.net.netty.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.module.PersonInfo;
import com.kyg.tjtoolmanager.net.netty.constant.LapiUriConstat;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.util.internal.StringUtil;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * HttpConnectionThread
 *
 * @author s04180
 * @Description
 */
public class RequestPersonInfoThread extends Thread {

    //终端接口地址URI
    private String api = LapiUriConstat.PEOPLE_INFO_URI;

    //终端设备的序列号 or  响应心跳的 deviceCode
    //210235C6LC323C000074
    //210235C3R0319B000026
    private String serialNo = "";
    private int allPerson = 0;

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    public void setAllPerson(int allPerson) {
        this.allPerson = allPerson;
    }

    @Override
    public void run() {
        try {
            if (!StringUtil.isNullOrEmpty(serialNo)) {
                int count = allPerson % 6 == 0 ? (allPerson / 6) : (allPerson / 6 + 1);
                for (int i = 0; i < count; i++) {
                    JSONObject postObject = new JSONObject();
                    postObject.put("Num", 0);
                    postObject.put("Limit", 6);
                    postObject.put("Offset", i * 6);

                    FullHttpRequest httpRequest = RequestDeviceManager.createRequestDeviceInfo(api, postObject.toJSONString(), HttpMethod.POST);

                    Map<String, String> map = HttpKeepAliveManager.getResponseMapByKeepAliveConnection(serialNo, httpRequest);
                    if (Objects.nonNull(map)) {
                        String response = JSON.toJSONString(map, SerializerFeature.PrettyFormat);
                        JSONObject jsonObject = JSON.parseObject(response);
                        JSONArray array = jsonObject.getJSONObject("Response").getJSONObject("Data")
                                .getJSONObject("PersonList").getJSONArray("PersonInfoList");
                        List<PersonInfo> personInfos = JSON.parseArray(array.toString(), PersonInfo.class);
//                        LiveEventBus.get(LiveEventKey.FACE_PERSON_LIST).post(personInfos);
                    } else {
                        SQLiteUtils.insertLog(api + "... ... 请求超时 ... ... ");
//                        LiveEventBus.get(LiveEventKey.FACE_PERSON_LIST).post(null);
                        return;
                    }
                }
            }
        } catch (Exception e) {
            SQLiteUtils.insertLog("{}" + e.getMessage());
        }
    }
}
